package net.dopan.pigframe.config.utils;

import java.util.ArrayList;
import java.util.List;

public class SubListUtils {

    public static List subList(int pageNum,int pageSize,List list){
        int totalCount = list.size();
        int pageCount;
        int m = totalCount % pageSize;

        if (m > 0) {
            pageCount = totalCount / pageSize + 1;
        } else {
            pageCount = totalCount / pageSize;
        }

        List<List<Integer>> totalList = new ArrayList<List<Integer>>();
        for (int i = 1; i <= pageCount; i++) {
            if (m == 0) {
                List<Integer> subList = list.subList((i - 1) * pageSize, pageSize * (i));
                totalList.add(subList);
            } else {
                if (i == pageCount) {
                    List<Integer> subList = list.subList((i - 1) * pageSize, totalCount);
                    totalList.add(subList);
                } else {
                    List<Integer> subList = list.subList((i - 1) * pageSize, pageSize * i);
                    totalList.add(subList);
                }
            }
        }
        List list1 = new ArrayList();
        try {
            list1 =totalList.get(pageNum-1);
        }catch (IndexOutOfBoundsException e){
            e.printStackTrace();
            return totalList.get(totalList.size() - 1);
        }
        return list1;
    }
}
